<?php

namespace app\common\model;

use think\Model;

class Goods extends Model
{

    /**
     * 根据商品id获取商品信息
     * @param $id
     * @return null|static
     */
    function fetch_by_id($id)
    {
        return self::get($id);
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection|\think\Paginator
     */
    function list_($param=[],$cond=[])
    {
        if (isset($param["search_"]) && $param["search_"]) {       #商品名称搜索
            $cond["goods_name"] = ["LIKE", "%" . $param["search_"] . "%"];
        }
        if (isset($param["status"]) && $param["status"] != "") {   #商品状态
            $cond["status"] = $param["status"];
        }
        if (isset($param["export"]) && $param["export"] == 1) {   #导出数据
            $r_data = $this->where($cond)->select();
        }else   #查询数据
        {
            $r_data = $this->where($cond)->paginate(30,false,['query'=>request()->param()]);
        }
        return $r_data;
    }

    function increase_sale($num)
    {
        return $this->setInc("sale", $num);
    }

    function dec_stock($num)
    {
        return $this->setDec("stock", $num);
    }

    /**
     * 新增商品
     * @param $param
     * @return Goods|null
     */
    public function add_goods($param)
    {
        $goods = new self();
        $goods->bar_code = isset($param["bar_code"]) ? $param["bar_code"] : 0;
        $goods->goods_name = isset($param["goods_name"]) ? $param["goods_name"] : 0;
        $goods->stock = isset($param["stock"]) ? $param["stock"] : 0;
        $goods->image = isset($param["image"]) ? $param["image"] : 0;
        $goods->content = isset($param["content"]) ? $param["content"] : 0;
        $goods->pre_price = isset($param["pre_price"]) ? $param["pre_price"] : 0;
        $goods->price = isset($param["price"]) ? $param["price"] : 0;
        $goods->seo = isset($param["seo"]) ? $param["seo"] : 0;
        $goods->place_of_origin = isset($param["place_of_origin"]) ? $param["place_of_origin"] : 0;
        $goods->weight = isset($param["weight"]) ? $param["weight"] : 0;
        $goods->seller_id = isset($param["seller_id"]) ? $param["seller_id"] : 0;
        $goods->created_time = date("Y-m-d H:i",time());
        $goods->save();
        return $goods;
    }

    /**
     * 编辑商品
     * @param $goods_id
     * @param $param
     * @return array|false|\PDOStatement|string|Model
     */
    public function edit_goods($goods_id,$param)
    {
        $goods = $this->find($goods_id);
        $goods->bar_code = isset($param["bar_code"]) ? $param["bar_code"] : 0;
        $goods->goods_name = isset($param["goods_name"]) ? $param["goods_name"] : 0;
        $goods->stock = isset($param["stock"]) ? $param["stock"] : 0;
        $goods->image = isset($param["image"]) ? $param["image"] : 0;
        $goods->content = isset($param["content"]) ? $param["content"] : 0;
        $goods->pre_price = isset($param["pre_price"]) ? $param["pre_price"] : 0;
        $goods->price = isset($param["price"]) ? $param["price"] : 0;
        $goods->seo = isset($param["seo"]) ? $param["seo"] : 0;
        $goods->place_of_origin = isset($param["place_of_origin"]) ? $param["place_of_origin"] : 0;
        $goods->weight = isset($param["weight"]) ? $param["weight"] : 0;
        if(isset($param['role_is_admin']) && !empty($param['role_is_admin']))
        {
            $goods->status = 1;
        }else
        {
            $goods->status = 0;
        }
        $goods->save();
        return $goods;
    }

    /**
     * 根据商品id删除此商品
     * @param $goods_id
     * @return int
     */
    public function del_goods_by_id($goods_id)
    {
        $data = $this->find($goods_id);
        if($data->delete())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 审核商品
     */
    public function goods_examine($goods_id,$param)
    {
        $goods = $this->find($goods_id);
        $goods->status = $param['goods_status'];
        $goods->save();
        return $goods;
    }

}
